﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Ledict.ConditionExtract.Controls
{
    public partial class gridViewColumn : Panel
    {
        public gridViewColumn()
        {
            InitializeComponent();
        }
        public gridViewColumn(String columnNames)
        {           
            InitializeComponent();
            CreateControls(columnNames);
        }

        public delegate void DeleteButtonClick(object sender);

        public event DeleteButtonClick DeleteBtnClicked;

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
        }

        ComboBox combox1 = new ComboBox();
        ComboBox combox2 = new ComboBox();
        ComboBox combox3 = new ComboBox();
        LinkLabel linkLabel1 = new LinkLabel();
        TextBox tb = new TextBox();

        private void CreateControls(string columnNames)
        {
            if (!string.IsNullOrEmpty(columnNames))
            {

                
                this.AutoSize = true;
                Graphics graph = CreateGraphics();              

                //表头下拉列表
                combox3.FormattingEnabled = true;
                combox3.Name = "combox3";
                string[] strs = columnNames.Split(new Char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                int i = strs[0].Length, j = 0;
                for (int k = 0; k < strs.Length; k++)
                {
                    if (strs[k].Length > i)
                    {
                        i = strs[k].Length;
                        j = k;
                    }
                }
                SizeF sizeF4 = graph.MeasureString(strs[j].ToString(), this.Font);
                combox3.Size = new Size((int)(Math.Ceiling(sizeF4.Width)) + 20, (int)(Math.Ceiling(sizeF4.Height)));
                combox3.Items.AddRange(strs);
                combox3.Left = 3;
                combox3.DropDownStyle = ComboBoxStyle.DropDownList;
                combox3.Top = this.Location.Y;                
                this.Controls.Add(combox3);

                //等于不等于，包含不包含
                combox1.FormattingEnabled = true;
                combox1.Items.AddRange(new object[] { "=", "<>", "in", "not in" });
                combox1.Name = "combox1";
                //计算字符串的像素尺寸
                SizeF sizeF = graph.MeasureString("不等于", this.Font);
                combox1.Size = new Size((int)(Math.Ceiling(sizeF.Width)) + 20, (int)(Math.Ceiling(sizeF.Height)));
                combox1.Left = combox3.Right + 5;
                combox1.Top = this.Location.Y;
                combox1.DropDownStyle = ComboBoxStyle.DropDownList;
                this.Controls.Add(combox1);

               //输入文本框
                tb.Width = 150;
                tb.Height = 16;
                tb.Left = combox1.Right + 5;
                tb.Top = this.Location.Y;               
                this.Controls.Add(tb);

                //and or
                combox2.FormattingEnabled = true;
                combox2.Items.AddRange(new object[] { "and", "or" });
                combox2.Name = "combox2";
                SizeF sizeF1 = graph.MeasureString("and", this.Font);
                combox2.Size = new Size((int)(Math.Ceiling(sizeF1.Width)) + 20, (int)(Math.Ceiling(sizeF1.Height)));
                combox2.Top = this.Location.Y;
                combox2.Left = tb.Right + 5;
                combox2.DropDownStyle = ComboBoxStyle.DropDownList;
                this.Controls.Add(combox2);

                //删除
                linkLabel1.Name = "linkLabel1";                
                linkLabel1.Text = "删除";
                SizeF sf = graph.MeasureString(linkLabel1.Text, this.Font);
                linkLabel1.Width = (int)(Math.Ceiling(sizeF.Width)) + 1;
                linkLabel1.Height = (int)(Math.Ceiling(sizeF.Height));
                linkLabel1.Left = combox2.Right + 5;
                linkLabel1.Top = this.Location.Y;
                linkLabel1.LinkClicked += new LinkLabelLinkClickedEventHandler(this.colLinkButton_Click);
                this.Controls.Add(linkLabel1);
                
            }
        }

        public string GetCondition()
        {
            StringBuilder st = new StringBuilder();
            foreach (var obj in this.Controls)
            {
                if (obj is ComboBox)
                {
                    if (string.IsNullOrEmpty(((ComboBox)obj).Text))
                    {
                        MessageBox.Show("请添加完整信息");
                        return "请添加完整信息";
                    }
                    //if (((ComboBox)obj).Name == "combox3")
                    //{
                    //    st.Append(" '" + ((ComboBox)obj).Text + "' ");
                    //}
                    //else
                    //{
                        st.Append(" " + ((ComboBox)obj).Text + " ");
                  //  }

                }
                if (obj is TextBox)
                {
                    if (string.IsNullOrEmpty(((TextBox)obj).Text))
                    {
                        MessageBox.Show("请添加完整信息");
                        return "请添加完整信息";
                    }
                    if (st.ToString().Contains("in") || st.ToString().Contains("not in"))
                    {
                        st.Append("( '" + ((TextBox)obj).Text + "') ");
                    }
                    else
                    {
                        st.Append(" '" + ((TextBox)obj).Text + "' ");
                    }
                }
               
            }
            return st.ToString();
        }

        private void colLinkButton_Click(object sender, EventArgs e)
        {
            DeleteBtnClicked(this);
        }
    }
}
